
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/message/subscribe/subscribe.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('CHANNEL_TYPE');
    yufp.custom.vue({
      el: '#templateSubscribe',
      data: function () {
        var me = this;
        return {
          formdata: {},
          height: yufp.custom.viewSize().height,
          messageType: '',
          channelType: '',
          subscribeType: '',
          subscribeTypeMap: {
            O: this.$t('subscribe.jg'),
            U: this.$t('subscribe.yh'),
            G: this.$t('subscribe.gw'),
            R: this.$t('subscribe.js')
          },
          channelTypes: [],
          channelTypeMap: {
            system: this.$t('subscribe.xtxx'),
            mobile: this.$t('subscribe.dx'),
            email: this.$t('subscribe.yj')
          },
          dialogVisible: false,
          urls: {
            dataUrl: backend.messageService + '/api/template/getChannelSubscribeList/',
            saveSubscribeUrl: backend.messageService + '/api/template/saveSubscribe/',
            getSubscribeUrl: backend.messageService + '/api/template/getSubscribe/',
            userUrl: backend.appOcaService + '/api/selectAllUser/'
          },
          tableColumns: [
            { label: this.$t('subscribe.xxlx'), prop: 'messageType', resizable: true },
            { label: this.$t('subscribe.ms'), prop: 'messageDesc', resizable: true },
            { label: this.$t('subscribe.qdlx'), prop: 'channelType', resizable: true }
          ],
          queryButtons: [
            {
              label: this.$t('subscribe.ss'),
              op: 'submit',
              type: 'primary',
              show: true,
              click: function (model, valid) {
                if (valid) {
                  var param = {
                    condition: JSON.stringify(model)
                  };
                  me.$refs.templateSubscribeTable.remoteData(param);
                }
              }
            },
            { label: this.$t('subscribe.zz'), op: 'reset', show: this.resetButton }
          ],
          queryFields: [
            { placeholder: this.$t('subscribe.xxlx'), field: 'messageType', type: 'input' },
            { placeholder: this.$t('subscribe.ms'), field: 'messageDesc', type: 'input' }
          ],
          activeFlag: '',
          mainGridFirst: {
            height: 270,
            checkbox: true,
            dataUrl: backend.appOcaService + '/api/selectAllUser/',
            tableColumns: [
              { label: this.$t('subscribe.yhm'), prop: 'userNo', resizable: true, width: 290 },
              { label: this.$t('subscribe.yhm'), prop: 'userName', resizable: true }
            ]
          },
          mainGridRole: {
            height: 270,
            checkbox: true,
            dataUrl: backend.appOcaService + '/api/selectAllRole/',
            tableColumns: [
              { label: this.$t('subscribe.js'), prop: 'roleId', resizable: true, width: 210 },
              { label: this.$t('subscribe.jsm'), prop: 'roleCode', resizable: true, width: 80 },
              { label: this.$t('subscribe.jsmc'), prop: 'roleName', resizable: true, width: 200 },
              { label: this.$t('subscribe.ssjg'), prop: 'orgId', resizable: true }
            ]
          },
          mainGridPost: {
            height: 270,
            checkbox: true,
            dataUrl: backend.appOcaService + '/api/selectAllDuty/',
            tableColumns: [
              { label: this.$t('subscribe.gw'), prop: 'dutyId', resizable: true, width: 210 },
              { label: this.$t('subscribe.gwm'), prop: 'dutyCde', resizable: true, width: 80 },
              { label: this.$t('subscribe.gwmc'), prop: 'dutyName', resizable: true, width: 200 },
              { label: this.$t('subscribe.ssjg'), prop: 'belongOrgId', resizable: true }
            ]
          },
          mainGridFifth: {
            Relationship: ''
          },
          orgUrl: backend.appOcaService + '/api/selectAllOrg/',
          orgRootId: yufp.session.org.code, // 根据节点ID,
          param: {}
        };
      },
      methods: {
        userSubscribe: function () {
          var em = this;
          var row = this.$refs.templateSubscribeTable.selections;
          if (row.length != 1) {
            this.$message({ message: this.$t('subscribe.qxzytsj'), type: 'warning' });
            return;
          }
          if (this.$refs.templateSubscribeTable.selections[0].channelType == '') {
            this.$message({ message: this.$t('subscribe.wpzmbwfdy'), type: 'warning' });
            return;
          }
          this.messageType = this.$refs.templateSubscribeTable.selections[0].messageType;
          this.channelTypes = this.$refs.templateSubscribeTable.selections[0].channelType.split(',');
          this.activeFlag = 'first';
          this.dialogVisible = true;
          this.$nextTick(function () {
            em.$refs.userTable.remoteData();
            em.$refs.dutyTable.remoteData();
            em.$refs.roleTable.remoteData();
            em.$refs.orgUsertree.remoteData();
          });
        },
        handleClick: function (tab, event) {
          var em = this;
          if (tab.name === 'first') {
            var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: 'U' };
            yufp.service.request({
              method: 'POST',
              url: this.urls.getSubscribeUrl,
              data: comitData,
              callback: function (code, message, response) {
                var infos = response.data.split(',');
                em.$refs.userTable.clearSelection();
                for (var i = 0; i < infos.length; i++) {
                  em.$refs.userTable.data.filter(function (item) {
                    if (item.userNo === infos[i]) {
                      em.$refs.userTable.toggleRowSelection(item, true);
                    }
                  });
                }
              }
            });
          } else if (tab.name === 'second') {
            var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: 'G' };
            yufp.service.request({
              method: 'POST',
              url: this.urls.getSubscribeUrl,
              data: comitData,
              callback: function (code, message, response) {
                var infos = response.data.split(',');
                em.$refs.dutyTable.clearSelection();
                for (var i = 0; i < infos.length; i++) {
                  em.$refs.dutyTable.data.filter(function (item) {
                    if (item.dutyId === infos[i]) {
                      em.$refs.dutyTable.toggleRowSelection(item, true);
                    }
                  });
                }
              }
            });
          } else if (tab.name === 'third') {
            var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: 'R' };
            yufp.service.request({
              method: 'POST',
              url: this.urls.getSubscribeUrl,
              data: comitData,
              callback: function (code, message, response) {
                var infos = response.data.split(',');
                em.$refs.roleTable.clearSelection();
                for (var i = 0; i < infos.length; i++) {
                  em.$refs.roleTable.data.filter(function (item) {
                    if (item.roleId === infos[i]) {
                      em.$refs.roleTable.toggleRowSelection(item, true);
                    }
                  });
                }
              }
            });
          } else if (tab.name === 'fourth') {
            var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: 'O' };
            yufp.service.request({
              method: 'POST',
              url: this.urls.getSubscribeUrl,
              data: comitData,
              callback: function (code, message, response) {
                ;
                var infos = response.data.split(',');
                var keys = [];
                for (var i = 0; i < infos.length; i++) {
                  keys.push(infos[i]);
                }
                em.$refs.orgUsertree.setCheckedKeys(keys);
              }
            });
          } else if (tab.name === 'fifth') {
            var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: 'X' };
            yufp.service.request({
              method: 'POST',
              url: this.urls.getSubscribeUrl,
              data: comitData,
              callback: function (code, message, response) {
                ;
                var infos = response.data.split(',');
                var keys = [];
                for (var i = 0; i < infos.length; i++) {
                  keys.push(infos[i]);
                }
                em.mainGridFifth.Relationship = keys[0];
              }
            });
          }
        },
        saveSubscribe: function (subscribeType, subscribeValue) {
          var me = this;
          if (me.channelType == '') {
            me.$message({ message: me.$t('subscribe.xzqdlx'), type: 'error' });
            return;
          }
          var comitData = { messageType: this.messageType, channelType: this.channelType, subscribeType: subscribeType, subscribeValue: subscribeValue };
          var saveUrl = me.urls.saveSubscribeUrl;
          yufp.service.request({
            url: saveUrl,
            data: comitData,
            method: 'POST',
            callback: function (code, message, response) {
              if (response.data == 0) {
                // me.dialogVisible = false;
                me.$message({ message: me.$t('subscribe.cg'), type: 'success' });
              } else {
                me.$message({ message: me.$t('subscribe.sb'), type: 'error' });
              }
            }
          });
        },
        saveUserSubscribe: function () {
          var users = '';
          if (this.$refs.userTable.selections.length >= 1) {
            for (var i = 0; i < this.$refs.userTable.selections.length; i++) {
              users = users + this.$refs.userTable.selections[i].userNo + ',';
            }
          }
          this.saveSubscribe('U', users);
        },
        saveOrgSubscribe: function () {
          var checks = this.$refs.orgUsertree.getCheckedKeys();
          var orgs = '';
          for (var i = 0; i < checks.length; i++) {
            orgs = orgs + checks[i] + ',';
          }
          this.saveSubscribe('O', orgs);
        },
        saveRoleSubscribe: function () {
          var roles = '';
          if (this.$refs.roleTable.selections.length >= 1) {
            for (var i = 0; i < this.$refs.roleTable.selections.length; i++) {
              roles = roles + this.$refs.roleTable.selections[i].roleId + ',';
            }
          }
          this.saveSubscribe('R', roles);
        },
        saveDutySubscribe: function () {
          var dutys = '';
          if (this.$refs.dutyTable.selections.length >= 1) {
            for (var i = 0; i < this.$refs.dutyTable.selections.length; i++) {
              dutys = dutys + this.$refs.dutyTable.selections[i].dutyId + ',';
            }
          }
          this.saveSubscribe('G', dutys);
        },
        saveRelationshipSubscribe: function () {
          var relationship = this.mainGridFifth.Relationship;
          this.saveSubscribe('X', relationship);
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
